package com.fuyao.cloud.admin.flow.listener;

import com.fuyao.cloud.admin.flow.model.ExtendHisprocinst;
import com.fuyao.cloud.admin.flow.service.flowable.IExtendHisprocinstService;
import com.fuyao.cloud.admin.flow.service.flowable.IExtendProcinstService;
import com.fuyao.cloud.common.core.flowable.enums.ProcessStatusEnum;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.HistoryService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.history.HistoricProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class GlobalProcistEndListener extends AbstractFlowableEngineEventListener {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Lazy
    @Resource
    private IExtendProcinstService extendProcinstService;
    @Lazy
    @Resource
    private IExtendHisprocinstService extendHisprocinstService;
    @Lazy
    @Resource
    private HistoryService historyService;

    public GlobalProcistEndListener() {
        logger.info("GlobalProcistEndListener init:初始化流程结束全局监听器。。。。。。");
    }

    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        FlowableEntityEventImpl flowableEntityEvent = (FlowableEntityEventImpl) event;
        String processInstanceId = flowableEntityEvent.getProcessInstanceId();
        //拷贝数据
        this.updateExtendInfoToHis(processInstanceId);
    }

    /**
     * 更新历史的扩展信息
     *
     * @param processInstanceId 流程实例id
     */
    private void updateExtendInfoToHis(String processInstanceId) {
        //1.更新历史的流程实例的扩展信息
        ExtendHisprocinst extendHisProcinst = extendHisprocinstService.findExtendHisprocinstByProcessInstanceId(processInstanceId);
        String processStatus;
        if (extendHisProcinst != null && !ProcessStatusEnum.ZZ.toString().equals(extendHisProcinst.getProcessStatus())) {
            processStatus = ProcessStatusEnum.BJ.toString();
            ExtendHisprocinst extendHisprocinst = new ExtendHisprocinst(processInstanceId, processStatus);
            extendHisprocinstService.updateAllStatusByProcessInstanceId(extendHisprocinst);
        }
        //流程结束信息如果是知会不用评论
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        //流程一发起就结束信息添加
        if (historicProcessInstance == null) {
            ExtendHisprocinst extendHisprocinst = new ExtendHisprocinst(processInstanceId, ProcessStatusEnum.BJ.toString());
            extendHisprocinstService.updateAllStatusByProcessInstanceId(extendHisprocinst);
        }

        //删除运行时的流程实例扩展信息
        extendProcinstService.deleteExtendProcinstByProcessInstanceId(processInstanceId);
    }
}
